import java.util.*;

public class Cinema {
    private String name;
    private Map<String, Hall> halls = new HashMap<>();
    private List<Schedule> schedules = new ArrayList<>();
    private Map<String, Movie> movies = new HashMap<>();

    public Cinema(String name) {
        this.name = name;
    }

    // 放映厅管理（仅系统管理员）
    public void addHall(User user, Hall hall) throws PermissionException {
        if (user == null || user.getRole() != Role.SYSTEM_ADMIN) {
            throw new PermissionException("只有系统管理员才能添加放映厅");
        }
        halls.put(hall.getName(), hall);
    }

    public void removeHall(User user, String hallName) throws PermissionException {
        if (user == null || user.getRole() != Role.SYSTEM_ADMIN) {
            throw new PermissionException("只有系统管理员才能删除放映厅");
        }
        halls.remove(hallName);
    }

    public Hall getHall(String hallName) {
        return halls.get(hallName);
    }

    public List<Hall> getAllHalls() {
        return new ArrayList<>(halls.values());
    }

    // 电影管理（仅店长）
    public void addMovie(User user, Movie movie) throws PermissionException {
        if (user == null || user.getRole() != Role.MANAGER) {
            throw new PermissionException("只有店长才能添加电影");
        }
        movies.put(movie.getId(), movie);
    }

    public void removeMovie(User user, String movieId) throws PermissionException {
        if (user == null || user.getRole() != Role.MANAGER) {
            throw new PermissionException("只有店长才能删除电影");
        }
        movies.remove(movieId);
    }

    public Movie getMovie(String movieId) {
        return movies.get(movieId);
    }

    public List<Movie> getAllMovies() {
        return new ArrayList<>(movies.values());
    }

    // 排片管理（仅店长）
    public void addSchedule(User user, Schedule schedule) throws PermissionException {
        if (user == null || user.getRole() != Role.MANAGER) {
            throw new PermissionException("只有店长才能添加排片");
        }
        schedules.add(schedule);
    }

    public void removeSchedule(User user, Schedule schedule) throws PermissionException {
        if (user == null || user.getRole() != Role.MANAGER) {
            throw new PermissionException("只有店长才能删除排片");
        }
        schedules.remove(schedule);
    }

    public List<Schedule> getSchedules() {
        return schedules;
    }

    // 售票（仅售票员）
    public void sellTicket(User user, Schedule schedule, int row, int col, String phone) throws PermissionException, TicketException {
        if (user == null || user.getRole() != Role.CLERK) {
            throw new PermissionException("只有售票员才能售票");
        }
        Hall hall = schedule.getHall();
        Seat seat = hall.getSeat(row, col);
        seat.sell(phone);
    }

    public String getName() {
        return name;
    }
} 